package com.ssm.blog.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.blog.pojo.Blog;
import com.ssm.blog.pojo.criteria.BlogCriteria;
import com.ssm.blog.service.CategoryService;
import com.ssm.blog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/list.html")
    @ResponseBody
    public ModelAndView blogList(@RequestParam(defaultValue = "1") Integer pageNum, BlogCriteria blogCriteria, Model model) {
        //查询的博客分页列表
        PageInfo<Blog> blogPageInfo = blogService.getBlogByCriteria(pageNum, blogCriteria);
        //页面上的分类列表
        model.addAttribute("pageInfo", blogPageInfo);
        return new ModelAndView("blog_page");
    }

    @RequestMapping("/view.html")
    public ModelAndView blogList(Integer id) {
        Blog blog = blogService.getBlogById(id);
        return new ModelAndView("blog_detail", "blog", blog);
    }

}
